# -*-makefile-*-
#
# by exa aka Eray Ozkural 2001
# see COPYING file that comes with this distribution

# Build Rules

# Generic exec build

define build-exec
	@echo Building executable: $@
	@echo from $^
	@echo LD flags: $(LDFLAGS)
	@echo Libraries used: $(LIBS) $($(notdir $@)_LIBS)
	@$(LIBTOOL) --mode=link $(LD) $(LDFLAGS) $(CXXFLAGS) $(LIBDIR) $(INCLUDEDIR) \
	       $^ -L$(workingdir) $(LIBS) $($(notdir $@)_LIBS) $(OUTPUTFLAG) $@
	@echo Installing executable in the current directory
	#@$(INSTALL_PROGRAM) $@ bin
	#-@ln -fs $@ bin
endef

define call-build-exec

	$$(build-exec)
endef

prog-deprule = $(call eval,$(1): checkdir $$($(1)_EXECFILE) install-output-$(1))
$(call map,prog-deprule,$(PROGRAMS))
programs: $(PROGRAMS)

prog-buildrule = $(call eval,$$($(1)_EXECFILE): $$($(1)_OBJFILES) $(call-build-exec))
$(call map,prog-buildrule,$(PROGRAMS))

define prog-output

	mkdir bin; cd bin; ln -fs ../$$^ .
endef

prog-output-rule = $(call eval,install-output-$(1): \
$$($(1)_EXECFILE) $(prog-output))
$(call map,prog-output-rule,$(PROGRAMS))


# Generic lib build

libraries: $(libinfo) $(LIBRARIES)

libinfo:
	@echo Libraries: $(LIBRARIES)

define build-lib
	@echo Building library: $@
	@echo From: $^
	@echo LD flags: $(LDFLAGS)
	$(LIBTOOL) --mode=link $(LD) $(LDFLAGS) $(CXXFLAGS) $(LIBDIR) $(INCLUDEDIR) \
	       $^ $(OUTPUTFLAG) $@
	@echo Installing library in the current directory
	#@$(INSTALL_PROGRAM) $@ lib
	#-@ln -fs $@ lib
endef

define call-build-lib

	$$(build-lib)
endef

lib-deprule = $(call eval,$(1): checkdir $$($(1)_LIBFILE) install-output-$(1))
$(call map,lib-deprule,$(LIBRARIES))

lib-buildrule = $(call eval,$$($(1)_LIBFILE): \
$$($(1)_OBJFILES) $(call-build-lib))
$(call map,lib-buildrule,$(LIBRARIES))

define lib-output

	-mkdir lib; cd lib; ln -fs ../$$^ .
endef

lib-output-rule = $(call eval,install-output-$(1): \
$$($(1)_LIBFILE) $(lib-output))
$(call map,lib-output-rule,$(LIBRARIES))

